$(function(){
    var productId = getQueryString('productId');
    var infoUrl='/shop/getProductById?productId='+productId;//获得商品详细信息以及类别
    var categoryUrl = '/shop/getProductCategoryList';
    var productPostUrl='/shop/modifyProduct';
    var isEdit=false;
    if(productId){
        getInfo(productId);
        isEdit=true;
    }else{
        getCategory()
        productPostUrl='/shop/addProduct';
    }
    function getCategory(){
        $.getJSON(categoryUrl,function(data){
           if(data.success){
               var productCategoryList = data.data;
               var optionHtml='';
               productCategoryList.map(function(item,index){
                   optionHtml+='<option data-value="'+item.productCategoryId+'">'
                   +item.productCategoryName+'</option>';
               });
               $('#category').html(optionHtml);
           }
        });
    }

    function getInfo(id){
        $.getJSON(infoUrl,function(data){
            if(data.success){
                var product = data.product;
                $('#product-name').val(product.productName);
                $('#product-desc').val(product.productDesc);
                $('#priority').val(product.priority);
                $('#normal-price').val(product.normalPrice);
                $('#promotion-price').val(product.promotionPrice);
                var optionSelected = product.productCategoryId;
                var optionHtml = '';
                var optionArr = data.productCategoryList;
                optionArr.map(function(item,index){
                    var isSelect = optionSelected === item.productCategoryId?'selected':'';
                    optionHtml+='<option data-value="'
                        + item.productCategoryId+'"'
                        + isSelect
                        +'>'
                        +item.productCategoryName
                        +'</option>';
                });
                $('#category').html(optionHtml);
            }
        });
    }

    $('.detail-img-div').on('change','.detail-img:last-child',function(){
        if($('.detail-img'.length)<6){
            $('.detail-img').append('<input type="file" class="detail-img">');
        }
    });

    $('#submit').click(function(){
        var productName = $('#product-name').val();
        var productDesc = $('#product-desc').val();
        var priority = $('#priority').val();
        var normalPrice = $('#normal-price').val();
        var promotionPrice = $('#promotion-price').val();
        var productCategoryId = $('#category').find('option').not(function(){
           return !this.selected;
        }).data('value');
        var formData = new FormData();
        var imgAddr = $('#small-img')[0].files[0];
        if(imgAddr==null){
            formData.append("file",null);
        }else {
            formData.append("file", imgAddr);
        }
        $('.detail-img').map(function(index,item){
           if($('.detail-img')[index].files.length>0){
               formData.append("file",$('.detail-img')[index].files[0]);
           }else{
               formData.append("file",null);
           }
        });

        var verifyCodeActual = $('#j_captcha').val();
        if(!verifyCodeActual){
            $.toast("请输入验证码");
            return;
        }
        formData.append("productName",productName);
        formData.append("productDesc",productDesc);
        formData.append("priority",priority);
        formData.append("normalPrice",normalPrice);
        formData.append("promotionPrice",promotionPrice);
        formData.append("productCategoryId",productCategoryId);
        formData.append("verifyCodeActual",verifyCodeActual);
        if(productId!=null){
            formData.append("productId",productId);
        }
        $.ajax({
            url:productPostUrl,
            enctype:'multipart/form-data',
            type:'POST',
            data:formData,
            contentType:false,
            processData:false,
            cache:false,
            success:function(data){
                if(data.success){
                    $.toast("提交成功!");
                }else{
                    $.toast("提交失败!"+data.errMsg);
                }
                $('#captcha_img').click();
            }
        });
    });
})



